using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Properties;

namespace svs.Mobile.BeyondMedia.Controls
{
	/// <summary>
	/// Provide a 2 line listview
	/// </summary>
	internal class OpmlView : FeedsView
	{

		protected const int Column1Left = 5;
		protected const int Column2Left = 25;

		public OpmlView() : base(false)
		{
			ItemSeparatorColor = Color.Silver;

			// We need a total of 5 rows, so the height is 180/5=36
			using (Graphics g = CreateGraphics())
			{
				using (Font font = new Font(Font.Name, FONT_SIZE, FontStyle.Bold))
				{
					// Calc line height to be height of letter A plus 10%
					int fontHeight = (int) (g.MeasureString("A", font).Height*1.1);
					ItemHeight = Math.Max(fontHeight, Resources.rss.Height + 2);
				}
			}
		}

		public override string OnEmptyText
		{
			get
			{
				return Resources.OpmlFeedIsEmpty;
			}
		}

		/// <summary>
		/// Custom OnPaint. This paints the listview items
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(PaintEventArgs e)
		{
			// Declare vars
			Font fontMainItem;
			Color fontColor;
			Bitmap bmp;

			if(Items.Count == 0)
			{
				OnPaintEmptyList(e);
				return;
			}

			using (Graphics gOffScreen = PrepareOffscreenGraphics())
			{
				// Set the y pos of the current item
				int itemTop = 0;

				// Draw the visible items.
				for (int n = VScrollBar.Value; n <= VScrollBar.Value + DrawCount; n++)
				{
					if (Items.Count == 0 || n > Items.Count - 1)
						break;

					// Get the current MailItem
					Feed feed = (Feed) Items[n];

					Rectangle itemArea;
					itemArea = new Rectangle(0,
					                         itemTop,
					                         ClientSize.Width - (VScrollBar.Visible ? VScrollBar.Width : 0),
					                         ItemHeight);

					// Draw the selected item to appear highlighted
					if (n == SelectedIndex)
					{
						DrawItemBox(gOffScreen,
						            ItemSeparatorColor,
						            true,
												false,
						            BackColor,
						            itemArea);

						fontColor = CalcTextColor(SystemColors.MenuText);
					}
					else if (n == TouchedIndex)
					{
						DrawItemBox(gOffScreen,
												ItemSeparatorColor,
												false,
												true,
												BackColor,
												itemArea);

						fontColor = ForeColor;
					}
					else
					{
						fontColor = ForeColor;
						DrawItemBox(gOffScreen,
						            ItemSeparatorColor,
						            false,
												false,
						            BackColor,
						            itemArea);
					}

					if (feed.Feeds.Count == 0)
						bmp = Resources.rss;
					else
						bmp = Resources.folder;

					ImageAttributes ia = new ImageAttributes();
					ia.SetColorKey(Color.Black, Color.Black);
					Rectangle imgRect = new Rectangle(Column1Left, itemTop + 3, bmp.Width, bmp.Height);
					gOffScreen.DrawImage(bmp, imgRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);

					//Draw the name of the feed
					fontMainItem =
						new Font(Font.Name, FONT_SIZE,
						         FeedRepository.GetFeedByUrl(feed.FeedUrl) == null ? FontStyle.Bold : FontStyle.Regular);
					Rectangle nameRect = new Rectangle(Column2Left, itemTop, itemArea.Width - Column2Left, ItemHeight);

					string name = feed.Name;
					if (name.Length > 40)
						name = name.Substring(0, 40) + "...";

					using (Brush b = new SolidBrush(fontColor))
					{
						gOffScreen.DrawString(name,fontMainItem,b, nameRect);
					}

					// Cleanup
					fontMainItem.Dispose();
					bmp.Dispose();

					// Set the next item top to move down the item height
					itemTop += ItemHeight + 1;
				}

				// Now draw the visible list box
				e.Graphics.DrawImage(OffScreen, 0, 0);
			}
			
		}

		protected override void FilterByCategory()
		{
			Items.AddRange(m_Feed.Feeds);
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Return:

					m_IsLocalKeyUp = true;
					e.Handled = true;
					break;

				case Keys.Left:
					e.Handled = true;
					break;

				//Left Right
				case Keys.Right:
					e.Handled = true;
					break;
			}

			if (!e.Handled)
				base.OnKeyDown(e);
		}
	}
}